package com.grapes.view;

import java.util.LinkedList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.grapes.R;
import com.grapes.business.CompanyRelativeBusiness;
import com.grapes.model.Student;
import com.grapes.widget.GrapesHeadIconImgView;

public class CompanyFavoritorActivity extends GrapesActivity {
	private List<Student> favoritors = new LinkedList<Student>();
	public static final String INTENT_EXTRA_COMPANY_ID = "CompanyId";
	public static final String INTENT_EXTRA_COMPANY_NAME = "companyName";
	private ListView favoritorList;
	public static final int MSG_LOAD_FAVORITOR_OK = 1;
	public static final int MSG_LOAD_FAVORITOR_FAIL = 2;

	public Handler hander = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case MSG_LOAD_FAVORITOR_OK:
			case MSG_LOAD_FAVORITOR_FAIL:
				dismissPorgressDialog();
				FavoriteAdapter adapter = new FavoriteAdapter();
				favoritorList.setAdapter(adapter);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.company_favoritors);
		String companyName = getIntent().getStringExtra(
				INTENT_EXTRA_COMPANY_NAME);
		TextView titleView = (TextView) findViewById(R.id.company_favoritor_title_text);
		titleView.setText(companyName);
		favoritorList = (ListView) findViewById(R.id.company_favoritor_list);
		final String companyId = getIntent().getStringExtra(
				INTENT_EXTRA_COMPANY_ID);
		showProgressDialog();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					favoritors = CompanyRelativeBusiness.getInstance()
							.getFakedCompanyFavoritors(companyId);
					Message msg = new Message();
					msg.what = MSG_LOAD_FAVORITOR_OK;
					hander.sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = MSG_LOAD_FAVORITOR_FAIL;
					hander.sendMessage(msg);
				}
			}
		}).start();
	}

	private class CellViewHolder {
		GrapesHeadIconImgView headIcon;
		TextView studentNameTxt;
		TextView studentSchoolNameTxt;
	}

	class FavoriteAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return favoritors.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CellViewHolder viewHolder;
			GrapesHeadIconImgView headIcon;
			TextView studentNameTxt;
			TextView studentSchoolNameTxt;

			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.company_favoritor_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.headIcon = (GrapesHeadIconImgView) convertView
						.findViewById(R.id.company_commentor_icon);
				viewHolder.studentNameTxt = (TextView) convertView
						.findViewById(R.id.company_commentor_name);
				viewHolder.studentSchoolNameTxt = (TextView) convertView
						.findViewById(R.id.company_favorotor_school);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			headIcon = viewHolder.headIcon;
			studentNameTxt = viewHolder.studentNameTxt;
			studentSchoolNameTxt = viewHolder.studentSchoolNameTxt;

			Student student = favoritors.get(position);
			headIcon.setResourceUrl(student.getIconUrl());
			studentNameTxt.setText(student.getStudentName());
			studentSchoolNameTxt.setText(student.getSchoolName());
			return convertView;
		}

	}
}
